package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.ui.widget.views.MyImageView;

import java.util.List;

/**
 * Created by yaohuix on 2016/3/21  14:17
 * <p/>
 * 说明：九宫格图片
 */
public class ImgGridAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<String> mDatas;

    public ImgGridAdapter(Context context, List<String> data) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = data;
    }

    public void update(List<String> list) {
        this.mDatas = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mDatas == null || mDatas.size() == 0) {
            return 0;
        }
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.layout_item_img, null);
            viewHolder = new MyViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MyViewHolder) convertView.getTag();
        }
        viewHolder.imageView.displayImage(mDatas.get(position));
        return convertView;
    }

    class MyViewHolder {
        MyImageView imageView;

        public MyViewHolder(View view) {
            imageView = (MyImageView) view.findViewById(R.id.miv_image);
        }
    }
}
